/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package news;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class News1 {

    public static String getRandomString(int length, String type) {
        String allStr = getString(type);
        String[] arrStr = allStr.split(splitStr);
        StringBuffer pstr = new StringBuffer();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                pstr.append(arrStr[new Random().nextInt(arrStr.length)]);
            }
        }
        return pstr.toString();
    }
    private static final String splitStr = " "; // 分割符  

    // 取数字字符串 用 splitStr 分割  
    private static String getNumberString() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 10; i++) {
            buf.append(String.valueOf(i));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取大写字母字符串 用 splitStr 分割  
    private static String getUppercase() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 26; i++) {
            buf.append(String.valueOf((char) ('A' + i)));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取小写字母字符串 用 splitStr 分割  
    private static String getLowercase() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 26; i++) {
            buf.append(String.valueOf((char) ('a' + i)));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取特殊字符串 用 splitStr 分割  
    private static String getSpecialString() {
        String str = "~@#$%^&*()_+|\\=-`";
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            buf.append(str.substring(i, i + 1));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 根据所取的字符串类型连接相应的字符串并返回  
    private static String getString(String type) {
        StringBuffer pstr = new StringBuffer();
        if (type.length() > 0) {
            if (type.indexOf('i') != -1) {
                pstr.append(getNumberString());
            }
            if (type.indexOf('l') != -1) {
                pstr.append(getLowercase());
            }
            if (type.indexOf('u') != -1) {
                pstr.append(getUppercase());
            }
            if (type.indexOf('s') != -1) {
                pstr.append(getSpecialString());
            }

        }
        return pstr.toString();
    }

    public static long getSecond(String time) throws ParseException {

        int second = 0;


        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm");
        Date date = format.parse(time);
        long ss = date.getTime();
        //System.out.println(ss);
        return ss;
    }
    News news = new News();

    public static void main(String arge[]) {
        long lasting = System.currentTimeMillis();
        String Sql = "";
        System.out.println(News.getRandomString(16, "ilu"));

        try {
            File f = new File("news.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(f);
            NodeList nl = doc.getElementsByTagName("topic");
            // String sql_insert = "";
            String template = "INSERT INTO supe_spacenews(nid, itemid,message,relativetags,postip,relativeitemids,customfieldid,customfieldtext,"
                    + "includetags,newsauthor,newsform,newsfromurl,newsurl,pageorder) VALUES ";
            System.out.println(template);
            for (int i = 0; i < nl.getLength(); i++) {
                //String temp = "insert into supe_spaceitems ()";
                String ff = "(#nid#, #itemid#,'##message##','a:0:{}','127.0.0.1','',0,'a:0:{}',"
                        + "'','','','','',1)";
                String temStr = "(";

                ff = ff.replaceAll("#hash#", News.getRandomString(16, "ilu"));
                if (doc.getElementsByTagName("tid").item(i).getFirstChild() != null) {
                    ff = ff.replaceAll("#nid#", doc.getElementsByTagName("tid").item(i).getFirstChild().getNodeValue());
                    ff = ff.replaceAll("#itemid#", doc.getElementsByTagName("tid").item(i).getFirstChild().getNodeValue());
                    temStr = temStr + doc.getElementsByTagName("tid").item(i).getFirstChild().getNodeValue() + ",";
                } else {
                    ff = ff.replaceAll("#itemid#", "1");
                }
                if (doc.getElementsByTagName("content").item(i).getFirstChild() != null) {
                    try{
                        ff = ff.replaceAll("##message##", doc.getElementsByTagName("content").item(i).getFirstChild().getNodeValue());
                    } catch(Exception e){
                        ff = ff.replaceAll("##message##", "");
                    }
                }

                System.out.println(ff + ",");

                ff = ff + ',';
                Sql += ff;
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("运行时间：" + (System.currentTimeMillis() - lasting) + " 毫秒");
        }

        //System.out.println(Sql);

        System.out.println(
                "运行时间：" + (System.currentTimeMillis() - lasting) + " 毫秒1");
    }
}
